"""
@File    :   swagger.py
@Time    :   2024/01/07 20:00:35
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   swagger资源离线加载
"""

from fastapi import FastAPI
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html


def static_app_api_docs(app: FastAPI):
    """让swagger的css与js文件从本地加载

    Args:
        app (FastAPI): Fastapi实例
    """
    from src.config import get_app_settings

    # 获取应用配置
    settings = get_app_settings()
    if settings.api_openapi_static_enable:
        if settings.api_docs_url:

            @app.get(settings.api_docs_url, include_in_schema=False)
            async def custom_swagger_ui_html():
                return get_swagger_ui_html(
                    openapi_url=app.openapi_url,  # type: ignore
                    title=app.title + " - Swagger UI",
                    oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
                    swagger_css_url=settings.static_prefix
                    + "/css/docs/swagger/swagger-ui.css",
                    swagger_js_url=settings.static_prefix
                    + "/js/docs/swagger/swagger-ui-bundle.js",
                    swagger_favicon_url=settings.static_prefix
                    + "/assets/docs/favicon.png",
                )

        if settings.api_redoc_url:

            @app.get(settings.api_redoc_url, include_in_schema=False)
            async def custom_redoc_ui_html():
                return get_redoc_html(
                    openapi_url=app.openapi_url,  # type: ignore
                    title=app.title + " - Redoc UI",
                    redoc_js_url=settings.static_prefix
                    + "/js/docs/redoc/redoc.standalone.js",
                    redoc_favicon_url=settings.static_prefix
                    + "/assets/docs/favicon.png",
                )
